package com.db.yupao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.db.yupao.common.BaseResponse;
import com.db.yupao.common.ErrorCode;
import com.db.yupao.common.ResultUtils;
import com.db.yupao.exception.BusinessException;
import com.db.yupao.model.dto.TeamQuery;
import com.db.yupao.model.pojo.Team;
import com.db.yupao.model.pojo.User;
import com.db.yupao.model.request.TeamAddRequest;
import com.db.yupao.service.TeamService;
import com.db.yupao.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 队伍接口
 */
@RestController
@RequestMapping("/api/team")
@Slf4j
public class TeamController {
    @Resource
    private UserService userService;

    @Resource
    private TeamService teamService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping("/add")
    public BaseResponse<Long> addTeam(@RequestBody TeamAddRequest teamAdd, HttpServletRequest request) {
        if (teamAdd == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        Team team = new Team();
        BeanUtils.copyProperties(teamAdd,team );
        long tenantId =  teamService.addTeam(team, loginUser);
        return ResultUtils.success(tenantId);
    }



    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTeam(@RequestBody Long teamId) {
        if (teamId <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = teamService.removeById(teamId);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return ResultUtils.success(true);
    }


    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeam(@RequestBody Team team) {
        if (team == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = teamService.updateById(team);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "更新失败");
        }
        return ResultUtils.success(true);
    }

    @GetMapping("/get")
    public BaseResponse<Team>  getTeamById(Long teamId){
        if (teamId <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Team team = teamService.getById(teamId);
        if (team == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        return ResultUtils.success(team);
    }

    @GetMapping("/list")
    public BaseResponse<List<Team>> getTeamList(TeamQuery teamQuery){
        if (teamQuery == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Team team = new Team();
        // 赋值拷贝
        try {
            BeanUtils.copyProperties(team,teamQuery);
        } catch (BeansException e) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        QueryWrapper<Team> queryWrapper  = new QueryWrapper<>(team);
        List<Team> teamList = teamService.list(queryWrapper);
        if (teamList == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        return ResultUtils.success(teamList);
    }


    @GetMapping("/list/page")
    public BaseResponse<Page<Team>> getTeamListByPage(TeamQuery teamQuery){
        if (teamQuery == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Team team = new Team();
        // 赋值拷贝
        try {
            BeanUtils.copyProperties(team,teamQuery);
        } catch (BeansException e) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        QueryWrapper<Team> queryWrapper  = new QueryWrapper<>(team);
        long pageNum = teamQuery.getPageNum();
        long pageSize = teamQuery.getPageSize();

        Page<Team> page =  new Page<>(pageNum,pageSize,pageNum*pageSize);
        Page<Team> resultPage = teamService.page(page, queryWrapper);
        if (resultPage == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        return ResultUtils.success(resultPage);
    }

}
